﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XCode;
using XCode.Configuration;
using NewLife.CommonEntity;
using V8Core.Entities.BLL;
using log = NewLife.CommonEntity.Log;
namespace V8.SystemSet
{
    public partial class Log : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        private void BindGrid()
        {
            Grid1.Title = log.Meta.Table.Description;
            foreach (FieldItem item in log.Meta.Fields)
            {
                ExtAspNet.BoundField boundField1 = new ExtAspNet.BoundField();
                boundField1.DataTooltipField = item.Name;
                boundField1.DataField = item.Name;
                boundField1.DataFormatString = "{0}";
                boundField1.HeaderText = item.Description;
                Grid1.Columns.Add(boundField1);
            }
            Grid1.DataSource = log.FindAll();
            Grid1.DataBind();
        }
        private void BindGrid(Int32 Key)
        {
            Grid1.Title = log.Meta.Table.Description;
            foreach (FieldItem item in log.Meta.Fields)
            {
                ExtAspNet.BoundField boundField1 = new ExtAspNet.BoundField();
                boundField1.DataTooltipField = item.Name;
                boundField1.DataField = item.Name;
                boundField1.DataFormatString = "{0}";
                boundField1.HeaderText = item.Description;
                Grid1.Columns.Add(boundField1);
            }
            Grid1.DataSource = log.FindAll();
            Grid1.DataBind();
        }

        protected void ddlSearchRange_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (ddlSearchRange.SelectedValue)
            {
                case "ALL":

                    break;
                default:
                    break;
            }
            BindGrid();
        }

        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            Grid1.SortColumnIndex = e.ColumnIndex;
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 从每个选中的行中获取Logid（在Grid1中定义的DataKeyNames）
            string ids = "";
            foreach (int rowIndex in Grid1.SelectedRowIndexArray)
            {
                if (ids == "")
                {
                    ids = (Grid1.DataKeys[rowIndex][0]).ToString();
                }
                else
                {
                    ids = ids + "," + (Grid1.DataKeys[rowIndex][0]).ToString();
                }
            }
            if ((ids != null))
            {
                ;
                log.Delete("ID In (" + ids + ")");
            }
            // 执行数据库操作
            //new Delete().From<XLog>()
            //     .Where(XLog.LogidColumn).In(ids)
            //     .Execute(); 

            // 清空当前选中的项
            Grid1.SelectedRowIndexArray = null;

            // 重新绑定表格
            BindGrid();
        }
        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            int id = Convert.ToInt32(Grid1.DataKeys[e.RowIndex][0]);

            if (e.CommandName == "Delete")
            {
                //XLog.Delete(id);

                BindGrid();
            }
        }

    }
}